﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RaidTeamManager.Model
{
    public class EFRaidManagerRepository : IRaidManagerRepository
    {
        private Dictionary<int, string> _cachedClasses;
        private Dictionary<int, string> CachedClasses
        {
            get { return _cachedClasses ?? (_cachedClasses = GetClasses()); }
        }

        public void AddRaider(RaiderInfo raiderInfo)
        {            
            using (var context = new RaidManagerDBEntities())
            {
                var realm = context.Realms.SingleOrDefault(r => r.Name == raiderInfo.RealmName);
                if (realm == null)
                {
                    realm = new Realm() { IsDefault = true, Name = raiderInfo.RealmName };
                    context.Realms.Add(realm);
                }
                var characterClass = CachedClasses.FirstOrDefault(c => c.Value == raiderInfo.Class);
                int classId = characterClass.Key;
                realm.Raiders.Add(new Raider { Name = raiderInfo.CharacterName, AverageItemLevel = raiderInfo.AverageItemLevel, ClassId = classId });                 
                context.SaveChanges();
            }
        }

        public List<RaiderInfo> GetRaiders()
        {
            var raiders = new List<RaiderInfo>();
            using (var context = new RaidManagerDBEntities())
            {
                raiders.AddRange(context.Raiders.ToList().Select(raider => new RaiderInfo() {CharacterName = raider.Name, RealmName = raider.Realm.Name, AverageItemLevel = raider.AverageItemLevel, Class = raider.Class == null ? string.Empty : raider.Class.Name}));
            }
            return raiders;
        }

        public string GetDefaultRealm()
        {
            var defaultRealm = string.Empty;
            using (var context = new RaidManagerDBEntities())
            {
                var realm = context.Realms.FirstOrDefault(r => r.IsDefault);
                if (realm != null)
                    defaultRealm = realm.Name;
            }
            return defaultRealm;
        }

        public Dictionary<int, string> GetClasses()
        {
            var classes = new Dictionary<int, string>();
            using (var context = new RaidManagerDBEntities())
            {
                foreach (var c in context.Classes.ToList())
                {
                    classes.Add(c.Id, c.Name);
                }
            }
            return classes;
        }

        public void AddClasses(Dictionary<int, string> classes)
        {
            using (var context = new RaidManagerDBEntities())
            {
                foreach (var c in classes)
                {
                    context.Classes.Add(new Class() {Id = c.Key, Name = c.Value});
                }
                context.SaveChanges();
            }            
        }

        public string GetClass(int classId)
        {
            string className;
            return CachedClasses.TryGetValue(classId, out className) ? className : string.Empty;
        }
        
    }
}
